home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / tests / syscalls / flock / flockTest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-24  |  4.5 KB  |  183 lines

  1. /*
  2.  * flockTest.c
  3.  *
  4.  * Test to make sure that the flock system call is working properly.
  5.  * If it works properly, this program will exit silently.  If an error
  6.  * occurs, a diagnostic will be printed to stderr.
  7.  *
  8.  * Copyright 1988 Regents of the University of California
  9.  * Permission to use, copy, modify, and distribute this
  10.  * software and its documentation for any purpose and without
  11.  * fee is hereby granted, provided that the above copyright
  12.  * notice appear in all copies.  The University of California
  13.  * makes no representations about the suitability of this
  14.  * software for any purpose.  It is provided "as is" without
  15.  * express or implied warranty.
  16.  */
  17.  
  18. #ifndef lint
  19. static char rcsid[] = "$Header: /sprite/src/tests/syscalls/flock/RCS/flockTest.c,v 1.1 90/05/24 10:31:26 rab Exp $";
  20. #endif
  21.  
  22. #include <stdio.h>
  23. #include <sys/file.h>
  24. #include <sys/wait.h>
  25. #include <signal.h>
  26. #include <string.h>
  27.  
  28. #define SHOULD_SUCCEED  0
  29. #define SHOULD_FAIL     1
  30. #define LOCKFILE        "LOCK"
  31. #ifndef __STDC__
  32. #define const
  33. #define volatile
  34. #endif
  35.  
  36. extern int errno;
  37. static const char *programName;
  38. static int errorFlag;
  39. static volatile int continueSignalFlag;
  40.  
  41. #ifdef __STDC__
  42. void main(int argc, const char **argv);
  43. void doLock(int mode);
  44. void gotContinueSignal(void);
  45. #else
  46. void main();
  47. void doLock();
  48. int gotContinueSignal();
  49. #endif
  50.  
  51. /*
  52.  *---------------------------------------------------------------------------
  53.  *
  54.  * main--
  55.  *
  56.  *      Forks off a child.  The child waits for the parent to send a signal
  57.  *      to proceed.  After the parent locks a file, it forks another child
  58.  *      which immediately exits.  This should not release the lock.  The
  59.  *      parent then signals the first child to proceed.  The first child
  60.  *      attempts to lock the file.  This should fail, since the parent should
  61.  *      still have it locked.
  62.  *
  63.  *  Results:
  64.  *      Zero exit code if no errors.  Non-zero otherwise.
  65.  *
  66.  *  Side effects:
  67.  *      If the program exits abnormally, it may leave the lock file
  68.  *      lying around.
  69.  *
  70.  *---------------------------------------------------------------------------
  71.  */
  72. /*ARGSUSED*/
  73. void
  74. main(argc, argv)
  75.     int argc;
  76.     const char **argv;
  77. {
  78.     union wait waitStatus;
  79.     int childPid;
  80.  
  81.     programName = *argv;
  82.     if ((childPid = fork()) == 0) {
  83.     (void) signal(SIGUSR1, gotContinueSignal);
  84.     while (continueSignalFlag == 0)    /* wait for parent to continue */
  85.         (void) pause();
  86.     doLock(SHOULD_FAIL);
  87.     exit(errorFlag);
  88.     } else {
  89.     doLock(SHOULD_SUCCEED);
  90.     if (fork() == 0)    /* create a second child that immediately dies */
  91.         exit(0);
  92.     (void) wait((union wait *) NULL);   /* wait for 2nd child to die */
  93.     (void) kill(childPid, SIGUSR1);     /* signal 1st child to continue */
  94.     while (wait(&waitStatus) > 0) {     /* wait for first child to die */
  95.         if (waitStatus.w_retcode)       /* check child's exit code */
  96.             ++errorFlag;
  97.     }
  98.     (void) unlink(LOCKFILE);
  99.     exit(errorFlag);
  100.     }
  101. }
  102.  
  103. /*
  104.  *---------------------------------------------------------------------------
  105.  *
  106.  *  doLock --
  107.  *
  108.  *      Opens a file called `LOCK' in the current directory, and then
  109.  *      attempts to lock it.  The input parameter indicates whether the
  110.  *      lock should succeed or fail.
  111.  *
  112.  *  Results:
  113.  *      none.
  114.  *
  115.  *  Side effects:
  116.  *      If the result was other than what was expected a diagnostic is
  117.  *      printed to stderr, and `errorFlag' is set.
  118.  *
  119.  *---------------------------------------------------------------------------
  120.  */
  121.  
  122. void
  123. doLock(mode)
  124.     int mode;
  125. {
  126.     int fd;
  127.  
  128.     if ((fd = open(LOCKFILE, O_RDWR|O_CREAT, 0666)) < 0) {
  129.     (void) fprintf(stderr, "%s: open failed: %s\n",
  130.         programName, strerror(errno));
  131.     ++errorFlag;
  132.     return;
  133.     }
  134.     if (flock(fd, LOCK_EX|LOCK_NB) < 0) {
  135.     if (mode == SHOULD_SUCCEED) {
  136.         (void) fprintf(stderr, "%s: flock failed: %s\n",
  137.         programName, strerror(errno));
  138.         ++errorFlag;
  139.         return;
  140.     }
  141.     } else {
  142.     if (mode == SHOULD_FAIL) {
  143.         (void) fprintf(stderr,
  144.         "%s: flock successful, should have failed.\n", programName);
  145.         ++errorFlag;
  146.         return;
  147.     }
  148.     }
  149.     return;
  150. }
  151.  
  152. /*
  153.  *---------------------------------------------------------------------------
  154.  *
  155.  * gotSignal --
  156.  *      
  157.  *      Called when the child gets a signal.
  158.  *
  159.  * Results:
  160.  *      None.
  161.  *
  162.  * Side effects:
  163.  *
  164.  *      Set a flag that indicates that a signal has been received.
  165.  *
  166.  *
  167.  *---------------------------------------------------------------------------
  168.  */
  169.  
  170. #ifdef __STDC__
  171. void 
  172. #else
  173. int
  174. #endif
  175. gotContinueSignal()
  176. {
  177.     continueSignalFlag = 1;
  178. #ifndef __STDC__
  179.     return 0;
  180. #endif
  181. }
  182.  
  183.